<?

function ranking_wowprogress10($guild=CFG_GUILDE){
	$url = "http://www.wowprogress.com/guild/".CFG_SERVEUR."/".CFG_ROYAUME."/".$guild."/rating.tier14_10/json_rank";
	for($i=0;$i<5; $i++){
		$json=ranking_getDataJSON($url);		
		if($json!=false) return $json;
		sleep(rand(1,2));
	}
	return false;
}

function ranking_wowprogress($guild=CFG_GUILDE){
	$url = "http://www.wowprogress.com/guild/".CFG_SERVEUR."/".CFG_ROYAUME."/".$guild."/json_rank";
	for($i=0;$i<5; $i++){
		$json=ranking_getDataJSON($url);		
		if($json!=false) return $json;
		sleep(rand(1,2));
	}
	return false;
}

function ranking_guildox($guild=CFG_GUILDE){
	$url = "http://guildox.com/go/guildxml.aspx?n=".$guild."&r=".CFG_ROYAUME."-".CFG_SERVEUR."&j=1";
	for($i=0;$i<5; $i++){
		$json=ranking_getDataJSON($url);		
		if($json!=false) return $json;
		sleep(rand(1,2));
	}
	return false;
}

function ranking_getDataJSON($url) {
	$f = "";
	if(function_exists('curl_init')) {
		$ch = curl_init();
		$timeout = 5; // set to zero for no timeout
		$useragent="Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.17 (KHTML, like Gecko) Chrome/24.0.1312.5 Safari/537.17";
		curl_setopt ($ch, CURLOPT_URL, $url);
		curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
		curl_setopt ($ch, CURLOPT_USERAGENT, $useragent);
		$str  = array(
			"Accept-Language: fr",
			"Accept-Charset: ISO-8859-1,utf-8;",
		);
		curl_setopt ( $ch, CURLOPT_HTTPHEADER, $str );
		curl_setopt ( $ch, CURLOPT_HEADER, 0 );
		curl_setopt ( $ch, CURLOPT_FOLLOWLOCATION, 0 );
		curl_setopt ( $ch, CURLOPT_FORBID_REUSE, 1 );
		curl_setopt ( $ch, CURLOPT_LOW_SPEED_LIMIT, 5 );
		curl_setopt ( $ch, CURLOPT_LOW_SPEED_TIME, $timeout );
		curl_setopt ( $ch, CURLOPT_TIMEVALUE, $timeout*3 );
		$f = curl_exec($ch);
		curl_close($ch);			
	} else {
		$f = file_get_contents($url);
	}
	$json = json_decode($f);
	return $json;
}

function ranking_display($guild=CFG_GUILDE){
	$wowprogress10 = ranking_wowprogress10();
	$wowprogress = ranking_wowprogress();
	$guildox = ranking_guildox();

	$lastKillImg = "http://www.guildox.com/go/images/".$guildox->guildox->guild->LastAchievement[0]->Icon;
	$lastKillBossName = $guildox->guildox->guild->LastAchievement[0]->Name;
	$lastKillDate = $guildox->guildox->guild->LastAchievement[0]->Date;
	$lastKillDate = new DateTime($lastKillDate);
	$lastKillDate = $lastKillDate->format("Y-m-d");	

	$html = "";
	$html .= "\n<div class='ranking_module'>";
	$html .= "\n\t<div class='ranking_module_raid_size'>Raid 10 (<span class='ranking_module_raid_span_25'>10+25</span>)</div>";
	$html .= "\n\t<div class='ranking_module_guildname'>".$guild."<br/><span class='ranking_module_guildserver'>".CFG_ROYAUME."-".CFG_SERVEUR."</span></div>";
	$html .= "\n\t<div class='ranking_module_extra'>";
	$html .= "\n\t\t<div class='ranking_module_sites'>";
	$html .= "\n\t<div class='ranking_module_progress'>".$guildox->guildox->guild->Progress."</div>";
	$html .= "\n\t\t\t<div class='ranking_module_site'><a href='http://www.wowprogress.com/guild/".CFG_SERVEUR."/".CFG_ROYAUME."/".$guild."/' target='_blank'>WoWProgress</a> : ".$wowprogress10->realm_rank." (<span class='ranking_module_raid_span_25'>".$wowprogress->realm_rank."</span>)</div>";
	$html .= "\n\t\t\t<div class='ranking_module_site'><a href='http://www.guildox.com/wow/guild/".CFG_SERVEUR."/".CFG_ROYAUME."/".$guild."' target='_blank'>GuildOx</a> : ".$guildox->guildox->guild->RealmRank10." (<span class='ranking_module_raid_span_25'>".$guildox->guildox->guild->RealmRank."</span>)</div>";
	$html .= "\n\t\t</div>";
	$html .= "\n\t\t<div class='ranking_module_lastkill'>";
	$html .= "<img alt='".$guild." last kill' src='".$lastKillImg."' />";
	$html .= "<span class='ranking_module_lastkill_bossname'><a href='http://fr.wowhead.com/achievement=".$guildox->guildox->guild->LastAchievement[0]->ID."' target='_blank'>".$lastKillBossName."</a></span><br/><span class='ranking_module_lastkill_date'>".$lastKillDate."</span>";
	$html .= "\n\t\t</div>";
	$html .= "\n\t</div>";
	$html .= "\n</div>";

	return $html;
}